/**
 * 全局Store
 */
import { makeAutoObservable } from "mobx";
import requests, { CancenToken } from "./xhr/axios";

export class GlobalStore {
  constructor() {
    makeAutoObservable(this, {}, { autoBind: true });
  }

  count = 0;

  increment = () => {
    this.count += 1;
  };

  cancelGetData = null;

  changeCancelGetData = (data) => (this.cancelGetData = data);

  clearCancelGetData = () => (this.cancelGetData = null);

  getData = async () => {
    // 取消请求
    if(this.cancelGetData){
      this.cancelGetData();
      this.clearCancelGetData();
    }
    requests({
      method: "get",
      url: "/data",
      cancelToken: new CancenToken((c) => {
        this.changeCancelGetData(c);
      }),
    })
      .then((res) => {
        this.clearCancelGetData();
        console.log(res);
      })
      .catch((e) => {
        this.clearCancelGetData();
        console.log(e);
      });
  };
}
